package cn.com.czpb.client.app;

import android.app.Activity;
import android.content.Context;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.umeng.analytics.MobclickAgent;

import cn.com.czpb.client.R;
import cn.com.czpb.client.custorm.CustomProgressDialog;
import cn.com.czpb.client.model.event.DayNightEvent;
import cn.com.czpb.client.services.NetWorkChangeReceiver;
import cn.com.czpb.client.utils.SPUtil;

import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;


public abstract class ToolBarActivityWhite extends BaseActivity {
    App app;
    TextView tvTitle;
    LinearLayout ll_back;
    LinearLayout ll_back_1;
    Toolbar toolbar;
    ImageView search_tool_bar;
    ImageView collection_tool_bar;
    ImageView share_tool_bar;
    public EditText et_send;
    public TextView tv_send;

    protected SPUtil spu;//
    protected Activity activity;
    protected FragmentManager fm;
    private CustomProgressDialog dialog;
    Boolean isNight;

    private InputMethodManager imm;
    protected ImmersionBar mImmersionBar;

    private IntentFilter intentFilter;
    private NetWorkChangeReceiver netWorkChangeReceiver;
    private LocalBroadcastManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.AppTheme_NoActionBar);
//        EventBus.getDefault().register(this);
        setContentView(R.layout.activity_tool_bar_white);
        activity = this;
        fm = getSupportFragmentManager();
        spu = SPUtil.getInstance();
        if (isImmersionBarEnabled()) {
            initImmersionBar();
            mImmersionBar = ImmersionBar.with(this);
            mImmersionBar.statusBarDarkFont(true).init();

        }
        initToolBar();
        initContentView();
        initView();
        initData();
        isNight = spu.getIsNight();
        //获取当前的屏幕亮度并设置
        spu.changeAppBrightness(activity, isNight);
        app = (App) getApplication();
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        initNetWork();


    }

    //实例化
    protected void initImmersionBar() {
        //在BaseActivity里初始化
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.init();
    }

    /**
     * 是否可以使用沉浸式
     * Is immersion bar enabled boolean.
     *
     * @return the boolean
     */
    protected boolean isImmersionBarEnabled() {
        return true;
    }

    private void initNetWork() {

        intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        netWorkChangeReceiver = new NetWorkChangeReceiver();
        activity.registerReceiver(netWorkChangeReceiver, intentFilter);
    }

    @Subscribe
    public void onEventMainThread(DayNightEvent event) {
        boolean mFlagNight = event.ismFlagSelectNight();
        spu.changeAppBrightness(this, spu.getIsNight());
    }

    /**
     * 初始化toolbar
     */
    private void initToolBar() {
        et_send=(EditText) findViewById(R.id.et_send);
        tv_send=(TextView) findViewById(R.id.tv_send);
        ll_back_1 = (LinearLayout) findViewById(R.id.ll_back_1);
        ll_back = (LinearLayout) findViewById(R.id.ll_back);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setVisibility(View.GONE);
        search_tool_bar = (ImageView) findViewById(R.id.search_tool_bar);
        collection_tool_bar = (ImageView) findViewById(R.id.collection_tool_bar);
        share_tool_bar = (ImageView) findViewById(R.id.share_tool_bar);
        toolbar.setPopupTheme(R.style.AppTheme_PopupOverlay);
        toolbar.setSubtitleTextColor(Color.WHITE);
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setOverflowIcon(getResources().getDrawable(R.mipmap.nav_more));
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        showBackButton();
    }
    //底部返回的显示隐藏
    public void setBackVisiable(boolean isShow) {
        if (isShow) {
            ll_back.setVisibility(View.VISIBLE);
        }else {
            ll_back.setVisibility(View.GONE);
        }
    }

    public void setTvTitle(String title) {
        toolbar.setTitle(title);
    }

    public void settitleBg() {
        toolbar.setBackgroundResource(R.color.white);

    }


    public void setToolBarVisiable(boolean isVisiable) {
        if (isVisiable) {
            toolbar.setVisibility(View.VISIBLE);
            return;
        }
        toolbar.setVisibility(View.GONE);
    }

    protected void hiteToolBarBack() {
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    }

    /**
     * 返回按钮显示
     */
    private void showBackButton() {
//        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//        toolbar.setNavigationIcon(R.drawable.back_icon);

        ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

    }

    /**
     * 设置布局
     */
    private void initContentView() {
        FrameLayout contentLayout = (FrameLayout) findViewById(R.id.content_layout);
        getLayoutInflater().inflate(setMyContentView(), contentLayout);
        ButterKnife.bind(this);
    }

    public void showDialog() {
//        if (dialog == null)
//            dialog = CustomProgressDialog.createDialog(activity, false);
//        dialog.show();
    }

    public void disMissDialog() {
//        if (dialog != null) {
//            if (dialog.isShowing()) {
//                dialog.dismiss();
//            }
//        }
    }

    public abstract int setMyContentView();//继承类的布局

    public void initView() {
    }//规范控件初始化

    public void initData() {
    }//规范数据初始化

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        if (dialog != null) {
            dialog.dismiss();
        }
        unregisterReceiver(netWorkChangeReceiver);
        super.onDestroy();

        this.imm = null;
        if (mImmersionBar != null)
            mImmersionBar.destroy();  //在BaseActivity里销毁
    }


    public void finish() {
        super.finish();
        hideSoftKeyBoard();
    }

    public void hideSoftKeyBoard() {
        View localView = getCurrentFocus();
        if (this.imm == null) {
            this.imm = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
        }
        if ((localView != null) && (this.imm != null)) {
            this.imm.hideSoftInputFromWindow(localView.getWindowToken(), 2);
        }
    }
//    @Override
//    protected void onStop() {
//        if (!Anti_hijackingUtils.checkActivity(this)){
//            TUtils.toast("广西云已进入后台运行");
//        }
//        super.onStop();
//    }
@Override
protected void onResume() {
    super.onResume();
    MobclickAgent.onResume(this);
}

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
